package com.example.farmrental.service;

import com.example.farmrental.entity.Machinery;
import com.example.farmrental.repository.MachineryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Optional;

/**
 * 农机服务层
 */
@Service
public class MachineryService {
    
    @Autowired
    private MachineryRepository machineryRepository;

    
    /**
     * 获取所有农机列表
     */
    public List<Machinery> getAllMachinery() {
        return machineryRepository.findAll();
    }
    
    /**
     * 根据ID获取农机
     */
    public Optional<Machinery> getMachineryById(Long id) {
        return machineryRepository.findById(id);
    }
    
    /**
     * 保存农机
     */
    public Machinery saveMachinery(Machinery machinery) {
        return machineryRepository.save(machinery);
    }
    
    /**
     * 删除农机
     */
    public void deleteMachinery(Long id) {
        machineryRepository.deleteById(id);
    }
    
    /**
     * 根据分类查询农机
     */
    public List<Machinery> getMachineryByCategory(String category) {
        return machineryRepository.findByCategory(category);
    }
    
    /**
     * 根据状态查询农机
     */
    public List<Machinery> getMachineryByStatus(String status) {
        return machineryRepository.findByStatus(status);
    }
    
    /**
     * 根据名称模糊查询农机
     */
    public List<Machinery> searchMachineryByName(String name) {
        return machineryRepository.findByNameContaining(name);
    }
    
    /**
     * 获取所有农机分类列表
     */
    public List<String> getAllCategories() {
        return machineryRepository.findDistinctCategories();
    }
}
